import {util} from './util';
import modelMap from '../model';


let dvaApp = null;

const init = (app) => {
    console.log('app...',app)
    dvaApp = app;
};

/**
 * 避免多次注入
 * @type {{}}
 */
const injectedModels = {};

const injectModel = (key, isDynamic) => {
    if (injectedModels[key]) {
        return;
    }
    const modelCreator = modelMap[key];
    if (!modelCreator) {
        console.warn('unknown model: ' + key);
        return;
    }
    injectedModels[key] = true;
    const model = modelCreator();
    const namespace = isDynamic ? key + '@' + util.randomString(5).toLowerCase() : key;
    model.namespace = namespace;
    dvaApp.model(model);
    return namespace;
};

const removeModel = (key) => {
    dvaApp.unmodel(key);
};

const modelUtil = {init, injectModel, removeModel};

export {modelUtil};
